package cn.zhou.jin.controller;

import cn.zhou.basic.util.AjaxResult;
import cn.zhou.basic.util.PageList;
import cn.zhou.jin.domain.Department;
import cn.zhou.jin.query.DepartmentQuery;
import cn.zhou.jin.service.IDepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    /*
    * put  post  delete get
    * */
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    public Department queryOne(@PathVariable("id") Long id ){

        return  departmentService.loadById(id);
    }
    @GetMapping
    public List<Department> queryAll(){
        System.out.println("-----------------");

        return  departmentService.loadAll();
    }




    @DeleteMapping("/{id}")
    public AjaxResult Remove(@PathVariable("id") Long id ){
        try {
            departmentService.remove(id);
            return new AjaxResult();
        }catch( Exception e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("苏喂苏喂苏喂");
        }
    }


    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
            return departmentService.queryPage(query);
    }


    @PutMapping
    public AjaxResult addorupt(@RequestBody Department department) {

        try {
            if(department.getId()==null){
                 departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new AjaxResult().setSuccess(false).setMessage("拉胯拉胯");

        }

    }
    @PatchMapping()
    public AjaxResult patchremove(@RequestBody List<Long> ids ){
        try {
            departmentService.patchremove(ids);
            return new AjaxResult();
        }catch( Exception e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("苏喂苏喂苏喂");
        }
    }
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return  departmentService.queryTree();
    }
}
